<template>
  <div class="blog">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>博客管理</el-breadcrumb-item>
      <el-breadcrumb-item>查询博客</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 白板 -->
    <div>
      <el-card class="blog-box">
        <!-- 搜索
        <el-row :gutter="20" type="flex" justify="end">
          <el-col :span="5">
            <el-input placeholder="请输入内容" prefix-icon="el-icon-search" v-model="word">
            </el-input>
          </el-col>
          <el-col :span="2">
            <el-button type="primary" icon="el-icon-search" @click="bsearch()"
              >搜索</el-button
            >
          </el-col>
        </el-row> -->
        <!-- 表格 -->
        <el-table :data="blogs" stripe style="width: 100%" border>
          <el-table-column type="index" label="编号"> </el-table-column>
          <el-table-column prop="title" label="标题"> </el-table-column>
          <el-table-column prop="intro" label="文章描述"> </el-table-column>
          <el-table-column prop="createTime" label="发布时间">
          </el-table-column>
          <el-table-column prop="user.nickName" label="作者"> </el-table-column>
          <el-table-column prop="sortName" label="分类"> </el-table-column>

          <el-table-column label="操作" width="210">
            <template slot-scope="scope">
              <el-button
                type="primary"
                icon="el-icon-view"
                @click="bdesc(scope.row.id, scope.row.user.nickName)"
              ></el-button>
              <el-button type="warning" icon="el-icon-edit" @click="bedit(scope.row.id)">
              </el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                @click="bdelete(scope.row.id)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageInfo.currentPage"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageInfo.totle"
        >
        </el-pagination>
      </el-card>
      <!-- 查看博客详情对话框 -->
      <el-dialog
        title="详情"
        :visible.sync="dialogVisible"
        width="50%"
        :before-close="handleClose"
      >
        <span style="color: #909399; font-weight: 600">类别:</span>
        <el-tag>{{ blog.sortName }}</el-tag>
        <el-divider></el-divider>

        <span style="color: #909399; font-weight: 600">标签:</span>
        <el-tag v-for="(label, index) in labels" :key="index" type="success">{{
          label.name
        }}</el-tag>
        <el-divider></el-divider>

        <!-- 博客标题 -->
        <h1>{{ blog.title }}</h1>
        <!-- 博客详情内容 -->
        <div class="main">
          <div class="intro main">
            {{ blog.intro }}
          </div>
          <div class="img main">
            <el-image
              style="width: 20%; height: 100px"
              :src="blog.imageUrl"
              :fit="fit"
            ></el-image>
          </div>
          <div class="content main">
            {{ blog.content }}
          </div>
        </div>
        <el-divider></el-divider>

        <span style="color: #909399; font-weight: 600">作者:</span
        ><span>{{ nickName }}</span>
        <el-divider></el-divider>

        <span style="color: #909399; font-weight: 600">发布时间:</span
        ><span>{{ blog.createTime }}</span>
        <el-divider></el-divider>

        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogVisible = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>

            <!-- 查看修改对话框 -->
      <el-dialog
        title="修改博客"
        :visible.sync="modifyVisible"
        width="70%"
        :before-close="handleModifyClose"
      >
         <el-form ref="blog" :model="blog" :rules="blogRule" label-width="80px">
          <el-row>
            <el-col :span="10">
              <el-form-item label="标题" prop="title">
                <el-input v-model="blog.title" name="title" :disabled="true"></el-input>
              </el-form-item>
              <el-form-item label="博客简介" prop="intro">
                <el-input type="textarea" v-model="blog.intro"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="2" :offset="1"> 
              <el-image
                style="width: 100px; height: 100px"
                :src="blog.imageUrl"
                fit="scale-down">
              </el-image>
            </el-col>
            <el-col :span="9" :offset="2">
              <el-upload
                ref="upload"
                action="http://127.0.0.1:8090/image"
                list-type="picture-card"
                :limit="1"
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove"
                :on-success="handleSuccess">
                <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="picVisible">
                <img width="100%" :src="blog.imageUrl" alt="">
                </el-dialog>
            </el-col>
          </el-row>
          <el-form-item label="类别">
            <el-select
              v-model="blog.sid"
              placeholder="请选择博客类别"
            >
              <el-option
                v-for="(sort, index) in sorts"
                :key="index"
                :label="sort.name"
                :value="sort.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="标签">
            <el-checkbox-group v-model="blog.labels">
              <el-checkbox
                v-for="(label, index) in labels"
                :key="index"
                :label="label.name"
              ></el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          <el-form-item label="博客内容" prop="content">
            <el-input type="textarea" v-model="blog.content"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit('blog')">修改博客</el-button>
            <el-button @click="onReset()">取消</el-button>
          </el-form-item>
        </el-form>

      </el-dialog>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      nickName: "",
      fit: "scale-down",
      dialogVisible: false,
      modifyVisible:false,
      picVisible:false, 
      pageInfo: { currentPage: 1, pageSize: 5, totle: 0 },
      blog: {
        uid: window.sessionStorage.getItem("token"),
        sid: 1,
        title: "",
        imageUrl: "",
        labels:[],
        content: "",
        intro: "",
      },
      labels: [],
      blogs: [],
      sorts:[],
      blogRule:{
        title: [
          { required: true, message: "请输入文章标题", trigger: "blur" },
        ],
        intro: [
          { required: true, message: "请输入文章简介", trigger: "blur" },
        ],
         content: [
          { required: true, message: "请输入文章内容", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    handleRemove(file) {
      this.$http.delete("image",{
        params:{
          realPath:file.response.data.realPath,
        }
      });
      this.$message({
        type:"success",
        message: "图片删除成功"
      });
    },
    handlePictureCardPreview(file) {
      this.picVisible = true;
    },
    handleSuccess(response){
      this.blog.imageUrl="http://127.0.0.1:8090/"+response.data.imageUrl;
    },
    async getAllLabels() {
      var d = await this.$http.get("labels");
      this.labels = d.data.labels;
    },
    async getAllSorts() {
      var d = await this.$http.get("sorts");
      this.sorts = d.data.sorts;
    },
    async getAllBlogs(currentPage, pageSize) {
      var d = await this.$http.get("blogs/" + currentPage + "/" + pageSize);
      this.blogs = d.data.blogs;
      this.pageInfo = d.data.pageInfo;
    },
    async getLabelsById(bid) {
      var d = await this.$http.get("labels/" + bid);
      this.labels = d.data;
    },
    async getBlogById(id) {
      var d = await this.$http.get("blog/" + id);
      this.blog = d.data;
    },
    async delBlogById(id) {
      var d = await this.$http.delete("blog/" + id);
      if (d.code == 200) {
        this.$message({
          message: "删除成功",
          type: "success",
        });
        this.getAllBlogs(this.pageInfo.currentPage, this.pageInfo.pageSize);
      } else {
        this.$message({
          message: "删除失败",
          type: "error",
        });
      }
    },

    bdesc(id, nickName) {
      this.getLabelsById(id);
      this.getBlogById(id);
      this.nickName = nickName;
      this.dialogVisible = true;
    },
    bedit(id) {
      this.getAllLabels();
      this.getBlogById(id);
      this.getAllSorts();
      this.modifyVisible=true;
    },
    bdelete(id) {
      this.$confirm("此操作将永久删除该博客, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.delBlogById(id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSizeChange(size) {
      this.pageInfo.pageSize = size;
      this.getAllBlogs(this.pageInfo.currentPage, size);
    },
    handleCurrentChange(page) {
      this.pageInfo.currentPage = page;
      this.getAllBlogs(page, this.pageInfo.pageSize);
    },
    handleModifyClose(){
      this.modifyVisible=false;
    },
    handleClose() {
      this.dialogVisible = false;
    },
     onReset(){
      this.modifyVisible = false;
    },
    onSubmit(formName) {
      this.$refs[formName].validate( valid=>{
         this.$confirm('此操作将永久修改该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          if(valid){
            var d=await this.$http.put('blog',this.blog);
            if(d.code==200){
              this.$message({
                type:"success",
                message:d.message
              });
              this.$refs['upload'].clearFiles();
            }
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消修改'
          });          
        });
      })
    },
  },
  mounted: function () {
    this.getAllBlogs(1, 5);
  },
};
</script>
<style scoped>
.el-table {
  margin-top: 20px;
}
.el-tag {
  margin: 10px 10px;
}
.main {
  margin: 15px 0;
}
.intro {
  font-size: 0.9em;
}
.content {
  font-size: 1.1em;
  line-height: 28px;
}
</style>